package com.uglyboys.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uglyboys.R;
import com.uglyboys.constantes.Constantes;
import com.uglyboys.customtweetreader.model.CustomTweet;
import com.uglyboys.iface.IF_TelaTweet;
import com.uglyboys.util.BBBSpyUtil;

public class TweetArrayAdapter extends ArrayAdapter<CustomTweet>{
	private final Context context;
	private final List<CustomTweet> values;
	private HashMap<Long, Drawable> imagens = new HashMap<Long, Drawable>();
	private IF_TelaTweet telaTweet;
	public int count;
	
	public List<CustomTweet> getValues() {
		return values;
	}
	

	public Context getContext() {
		return context;
	}


	public TweetArrayAdapter(Context context, List<CustomTweet> values, IF_TelaTweet telaTweet) {
		super(context, R.layout.layout_tweets, values);
		this.context = context;
		this.values = values;
		this.telaTweet = telaTweet;
		count = values.size();
	}
	
	
	@Override
	public void clear() {
		this.imagens.clear();
		this.values.clear();
		super.clear();
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.layout_tweets, parent, false);
		
		TextView textConteudo = (TextView) rowView.findViewById(R.id.autor);
		TextView textData = (TextView) rowView.findViewById(R.id.data);
		
		final CustomTweet tweet = values.get(position);
		
		textConteudo.setTextColor(Color.BLUE);
		String texto = BBBSpyUtil.formatarTexto(tweet.getTexto(), tweet.getUsuario());
		textConteudo.setText(Html.fromHtml(texto));
		textConteudo.setTextSize(Constantes.FONTE_GRANDE);
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		textData.setTextSize(Constantes.FONTE_PEQUENA);
		textData.setText(sdf.format(tweet.getDataCriacao()));
		
		final ImageView imageView = (ImageView) rowView.findViewById(R.id.usuario);
		new Thread(new Runnable() {
			public void run() {
				
				final Drawable image;
				if(imagens.get(tweet.getId()) == null){
					image = ImageOperations(tweet.getUrlImagemPerfil(), "image.jpg");
					imagens.put(tweet.getId(), image);
				}else{
					image = imagens.get(tweet.getId());
				}
				telaTweet.runOnUiThread(new Runnable() {
					public void run() {
						imageView.setImageDrawable(image);
					}
				});
			}

		}).start();
		
		return rowView;
	}
	
	private Drawable ImageOperations(String url, String saveFilename) {
		try {
			InputStream is = (InputStream) this.fetch(url);
			Drawable d = Drawable.createFromStream(is, "src");
			return d;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public HashMap<Long, Drawable> getImagens() {
		return imagens;
	}


	public void setImagens(HashMap<Long, Drawable> imagens) {
		this.imagens = imagens;
	}


	public Object fetch(String address) throws MalformedURLException,
			IOException {
		URL url = new URL(address);
		Object content = url.getContent();
		return content;
	}

}
